<?php

namespace App\Http\Controllers\Activity;

use App\App\Services\Web\UserService;
use App\Contracts\Repositories\ActivityRepository as ActivityRepositoryContact;
use App\Contracts\Services\Storage\StorageService as StorageServiceContract;
use App\Entities\Activity;
use BadMethodCallException;
use Illuminate\Container\Container;
use Illuminate\Http\Request;

class ManagementController extends BaseController
{
    public function __construct(UserService $user, ActivityRepositoryContact $activities)
    {
        parent::__construct($user, $activities);
    }

    public function parseRules(Request $request)
    {
        throw new BadMethodCallException();
    }

    public function parseLimits(Request $request)
    {
        throw new BadMethodCallException();
    }

    public function show($id)
    {
        $activity = $this->activities->findOne($id);

        if (empty($activity)) {
            return response()->failed('没有找到该活动');
        }

        $storage = Container::getInstance()->make(StorageServiceContract::class);

        $translated = [
            'id' => $activity->id,
            'model_id' => $activity->model_id,
            'template_id' => $activity->template_id,
            'category' => $activity->category,
            'group_code' => $activity->group_code,
            'name' => $activity->name,
            'start_time' => $activity->startTime,
            'end_time' => $activity->endTime,
            'is_pause' => !!$activity->isPause,
            'clicks' => $activity->clicks,
            'joins' => $activity->joins,
            'winnings' => $activity->winnings,
            'limit' => \GuzzleHttp\json_decode($activity->limit),
            'background_img' => $storage->toHttpUrl($activity->backgroundImg),
            'background_color' => $activity->backgroundColor,
            'rule' => \GuzzleHttp\json_decode($activity->rule),
            'description' => $activity->description,
            'shares' => $this->formatShares($activity->shares),
            'created_at' => $activity->created_at,
            'updated_at' => $activity->updated_at,
            'list_background_img' => $storage->toHttpUrl($activity->listBackgroundImg),
            'rule_description' => $activity->ruleDescription,
            'app_url' => $activity->app_url,
        ];

        return response()->success($translated);
    }

    public function update(Request $request, $id)
    {
        throw new BadMethodCallException();
    }

    public function getRulesForCreate()
    {
        return [];
    }

    public function getRulesForUpdate()
    {
        return [];
    }

    protected function createdEvents( Activity $activity)
    {
        // TODO: Implement createdEvents() method.
    }

    protected function updatedEvents(Activity $activity)
    {
        // TODO: Implement updatedEvents() method.
    }
}
